/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.web;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.javaeye.delivery.dto.Logistics;
import com.javaeye.delivery.service.ILogisticsService;
import com.javaeye.common.util.ListItem;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.web.PageInfo;

public class LogisticsAction extends ActionSupport {
	
	private static Log log = LogFactory.getLog(LogisticsAction.class); 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3216585574200112708L;
	
	private List<ListItem> logisticsTypeList;

	/**
	 * 用于显示一览结果集
	 */
	private List<Logistics> logisticsList;
	
	/**
	 * 用于接收分页值对象
	 */
	private PageInfo pageInfo;
	
	/**
	 * 用于接收查询条件值对象
	 */
	private Logistics condition;
	
	/**
	 * 用于接收添加画面和编辑画面的值对象
	 */
	private Logistics logistics;
	
	/**
	 * 用于接收需要修改和删除的ID值
	 */
	private String logisticsId;
	
	/**
	 * 应用autocomplete的查询使用接口
	 */
	private String q;
	
	/**
	 * Service
	 */
	private ILogisticsService logisticsService;
	
	public LogisticsAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new Logistics();
		condition.setName("");
		condition.setCategory("-1");
	}
	
	public String queryAutoCompleteNames() {
		logisticsList = logisticsService.getAutoCompleteLogistics(q, 10);
		return SUCCESS;
	}
	
	/**
	 * 条件分页查询
	 * @return
	 * @throws Exception
	 */
	public String queryLogisticsList() throws Exception {
		log.debug("currentPage=" + pageInfo.getPageIndex() + 
				", category=" + condition.getCategory() + 
				", name=" + condition.getName());
		logisticsTypeList = ListUtil.logisticsTypeList();
		logisticsList = logisticsService.getLogisticsList(condition, pageInfo);
		for (Logistics l : logisticsList) {
			l.setCategory(ListUtil.logisticsTypeName(l.getCategory()));
		}
		return SUCCESS;
	}
	
	/**
	 * 保存产品
	 * @return
	 * @throws Exception
	 */
	public String saveLogistics() throws Exception {
		logistics = logisticsService.saveLogistics(logistics);
		return SUCCESS;
	}
	
	public String queryForAddLogistics() throws Exception {
		logisticsTypeList = ListUtil.logisticsTypeList();
		return SUCCESS;
	}
	
	/**
	 * 查询产品
	 * @return
	 * @throws Exception
	 */
	public String queryLogisticsForEdit() throws Exception {
		logisticsTypeList = ListUtil.logisticsTypeList();
		logistics = logisticsService.getLogistics(logisticsId);
		return SUCCESS;
	}
	
	/**
	 * 删除产品
	 * @return
	 * @throws Exception
	 */
	public String deleteLogistics() throws Exception {
		logisticsService.removeLogistics(logisticsId);
		return SUCCESS;
	}

	public List<Logistics> getLogisticsList() {
		return logisticsList;
	}

	public void setLogisticsList(List<Logistics> logisticsList) {
		this.logisticsList = logisticsList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public Logistics getCondition() {
		return condition;
	}

	public void setCondition(Logistics condition) {
		this.condition = condition;
	}

	public Logistics getLogistics() {
		return logistics;
	}

	public void setLogistics(Logistics logistics) {
		this.logistics = logistics;
	}

	public String getLogisticsId() {
		return logisticsId;
	}

	public void setLogisticsId(String logisticsId) {
		this.logisticsId = logisticsId;
	}

	public void setLogisticsService(ILogisticsService logisticsService) {
		this.logisticsService = logisticsService;
	}

	public void setLogisticsTypeList(List<ListItem> logisticsTypeList) {
		this.logisticsTypeList = logisticsTypeList;
	}

	public List<ListItem> getLogisticsTypeList() {
		return logisticsTypeList;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}
	
}
